﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GLImp;
using System.Drawing;
using CastleOfTheDisillusioned.Input;
using CastleOfTheDisillusioned.Location;

namespace CastleOfTheDisillusioned.Overworld {
	class Player {
		public int X;
		public int Y;

		public double DrawnX;
		public double DrawnY;

		public Player(int X, int Y) {
			this.X = X;
			this.Y = Y;

			this.DrawnX = this.X;
			this.DrawnY = this.Y;
		}


		public void Draw() {
			GraphicsManager.DrawRectangle(DrawnX + 0.25, DrawnY + 0.25, .5, .5, Color.Red);
		}

		public void Update() {
			//Movement through Overworld
			if (Controls.IsPressed(Control.Right)) {
				Move(X + 1, Y);
			}
			if (Controls.IsPressed(Control.Left)) {
				Move(X - 1, Y);
			}

			if (Controls.IsPressed(Control.Up)) {
				Move(X, Y - 1);
			}
			if (Controls.IsPressed(Control.Down)) {
				Move(X, Y + 1);
			}

			X = Math.Max(X, 0);
			X = Math.Min(X, Overworld_State.CurrentRealm.Width);
			
			//Play catchup with drawn copy
			DrawnX -= Math.Sign(DrawnX - X) * 0.1;
			if (Math.Abs(DrawnX - X) < 0.1) DrawnX = X;
			DrawnY -= Math.Sign(DrawnY - Y) * 0.1;
			if (Math.Abs(DrawnY - Y) < 0.1) DrawnY = Y;

			//Enter Location
			if (Controls.IsPressed(Control.EnterLocation)) {
				Location_State.EnterLocation(Overworld_State.CurrentRealm.GetTile(X, Y).Map);
			}
		}

		//Moves to a location if and only if it is visible
		private void Move(int X, int Y) {
			Realm r = Overworld_State.CurrentRealm;
			if (r.PlaceVisible(X, Y)) {
				this.X = X;
				this.Y = Y;
			}
		}
	}
}
